#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
/*
功能描述：
    按条件统计元素个数
函数原型：
    count_if(iterator beg,iterator end,_Pred);
    按条件统计元素出现次数
    beg开始迭代器
    end结束迭代器
    _Pred谓词
*/
class Greater20
{
public:
    bool operator()(int val)
    {
        return val>20;
    }
};
void test01()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(10);
    v.push_back(30);
    v.push_back(30);
    v.push_back(50);
    v.push_back(40);
    v.push_back(60);
    int num=count_if(v.begin(),v.end(),Greater20());//5
    cout<<"num="<<num<<endl;
}
//统计自定义数据类型
class Person 
{
public:
    Person(string name,int age)
    {
        this->m_Name=name;
        this->m_Age=age;
    }
    string m_Name;
    int m_Age;
};
class AgeGreater20
{
public:
    bool operator()(const Person &p)
    {
        return p.m_Age>20;
    }
};
void test02()
{
    vector<Person> v;
    Person p1("刘备",35);
    Person p2("关羽",35);
    Person p3("曹操",35);
    Person p4("赵云",45);
    Person p5("张飞",55);
    //将人员插入到容器中
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    //统计大于20岁人员个数
    int num=count_if(v.begin(),v.end(),AgeGreater20());
    cout<<"大于20岁人员人数为："<<num<<endl;//5
}
int main()
{
    test01();
    test02();
}